<?php
namespace App\Models;
use App\Constants\ErrorResponse;
use App\Filters\PostFilter;
use App\Filters\QueryFilter;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Laravel\Scout\Searchable;

/**
 * Copyright (C), 2021-2021, old_liu_cms.
 * FileName: Category.php
 * Description: 说明
 *
 * @author old_liu_cms
 * @Create Date    2021/10/10 16:27
 * @Update Date    2021/10/10 16:27 By old_liu_cms
 * @version v1.0
 */

class Post extends Model {
    use SoftDeletes;
    use Searchable;
    protected $fillable = [
        'is_top','is_hot','pub_time','title','summary','content','views_count','comments_count','category_id','test'
    ];

    public function scopeFilter($query, QueryFilter $filters)
    {
        return $filters->apply($query);
    }

    /**
     * FunctionName：lists
     * Description：列表
     * Author：old_liu_cms
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
     */
    public static function lists($filter)
    {
        return self::query()->filter($filter)->paginate(20);
    }

    /**
     * FunctionName：store
     * Description：创建
     * Author：lwl
     * @param array $params
     * @return null
     */
    public static function store(array $params)
    {
        if (!self::query()->create($params)) {
            errorHttpResponse(ErrorResponse::ERROR_OPERATION_FAIL);
        }
        return null;
    }

    /**
     * FunctionName：show
     * Description：详情
     * Author：lwl
     * @param int $id
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public static function show(int $id)
    {
        if (!$result = self::query()->whereKey($id)->first()) {
            errorHttpResponse(ErrorResponse::ERROR_DATA_NOT_EXISTS);
        }
        return $result;
    }

    /**
     * FunctionName：edit
     * Description：更新
     * Author：lwl
     * @param array $params
     * @return int
     */
    public static function edit(array $params)
    {
        $result = self::detail($params['id']);
        if (!$result->update($params)) {
            errorHttpResponse(ErrorResponse::ERROR_OPERATION_FAIL);
        }
        return null;
    }

    /**
     * FunctionName：remove
     * Description：删除
     * Author：lwl
     * @param int $id
     * @return null
     */
    public static function remove(int $id)
    {
        $result = self::show($id);
        if (!$result->delete($id)) {
            errorHttpResponse(ErrorResponse::ERROR_OPERATION_FAIL);
        }
        return null;
    }

    /**
     * FunctionName：getIsHotAttribute
     * Description：热度
     * Author：lwl
     * @param $val
     * @return bool
     */
    public function getIsHotAttribute($val):bool
    {
        return $val == 1;
    }

    /**
     * FunctionName：getIsTopAttribute
     * Description：置顶
     * Author：lwl
     * @param $val
     * @return bool
     */
    public function getIsTopAttribute($val):bool
    {
        return $val == 1;
    }

    /**
     * 指定索引
     * @return string
     */
    public function searchableAs()
    {
        return 'post-test';
    }

    /**
     * 设置导入索引的数据字段
     * @return array
     */
    public function toSearchableArray()
    {
//        dd(333);
        return [
            'text' => self::query()
                ->where('id',$this->id)
                ->value('test'),
            'title' => self::query()
                ->where('id',$this->id)
                ->value('title'),
//            'tags'    => implode(',',$this->tags),
//            'title'   => $this->title
        ];
    }

    /**
     * 指定 搜索索引中存储的唯一ID
     * @return mixed
     */
    public function getScoutKey()
    {
        return $this->id;
    }

    /**
     * 指定 搜索索引中存储的唯一ID的键名
     * @return string
     */
    public function getScoutKeyName()
    {
        return 'id';
    }
}
